package com.ytjj.common.utils;

import com.github.wxpay.sdk.WXPayConfig;
import com.ytjj.common.constants.WxConstants;
import org.springframework.context.annotation.Configuration;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * 微信app支付
 *
 *
 * @version 1.0
 * 
 */
@Configuration
public class WXAppConfigUtil implements WXPayConfig {

    private byte[] certData;
////    public static final String KEY = "P7975F4F3CB5ABWF9A2FED4D0M2663L4";
//    public static final String KEY = "63a461eb59deaa7a144c41bf0436f16d";
    private static WxConstants wxConstants;

    public void setWxConstants(WxConstants wxConstants){
        this.wxConstants = wxConstants;
    }

    public WxConstants getWxConstants(){
        return wxConstants;
    }

    public WXAppConfigUtil(){}

    public WXAppConfigUtil(String certPath) throws Exception {
        File file = new File(certPath);
        InputStream certStream = new FileInputStream(file);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    @Override
    public String getAppID() {
        return wxConstants.getWX_APP_ID();
    }

    @Override
    public String getMchID() {
        return wxConstants.getWX_MCH_ID();
    }

    @Override
    public String getKey() {
        return wxConstants.getWX_KEY();
    }

    @Override
    public InputStream getCertStream() {
        ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }

    @Override
    public int getHttpConnectTimeoutMs() {
        return 0;
    }

    @Override
    public int getHttpReadTimeoutMs() {
        return 0;
    }
}
